<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <div class="result-library-op2__main">
    <!-- <div class="left"> -->
      <el-card shadow="never" class="left">
        <el-input v-model="queryName" placeholder="请输入部段名称" clearable style="margin-bottom: 10px">
          <template #append>
            <el-button :icon="Search" @click="filterTree" />
          </template>  
        </el-input>
        <el-tree
          ref="resultLibraryTree"
          :data="treeData"
          node-key="index"
          highlight-current
          :props="defaultProps"
          :default-expand-all="false"
          :current-node-key="currentChooseId"
          :filter-node-method="filterNode"
          icon-class="el-icon-arrow-right"
          @node-click="treeSelect"
        >
        <template #default="{ data }">
          <span style="display: flex; align-items: center">
            <span class="sub_title"> {{ data.name }} {{ data.versionNumber ? ' / '+data.versionNumber : '' }}</span>
          </span>
        </template>
      </el-tree>
    </el-card>
    <!-- </div> -->
    <div class="right">
      <div class="query">
        <el-form inline ref="queryFormRef" :model="queryParams" label-width="120px" size="small">
          <el-form-item label="上传日期:">
            <el-date-picker
              v-model="dateRangeCreateTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            ></el-date-picker>
          </el-form-item>
          <!-- <el-form-item label="机型:">
            <el-select v-model="queryParams.modelNumber" placeholder="请选择机型">
              <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="批次:">
            <el-select v-model="queryParams.batchNumber" placeholder="请选择机型">
              <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="机型：">
              <el-select v-model="queryParams.modelNumber" clearable placeholder="请选择机型" @change="modelChange">
                <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="批次号：">
              <el-select v-model="queryParams.batchNumber" clearable placeholder="请选择批次号">
                <el-option v-for="(item, index) in batchList" :label="batchList[index]" :value="batchList[index]" :key="index"></el-option>
              </el-select>
            </el-form-item>
          <el-form-item label="数据质量检测状态:">
            <el-select v-model="queryParams.qualityCheck">
              <el-option label="通过" :value="0"></el-option>
              <el-option label="不通过" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="query_btns">
            <el-button size="small" type="primary" icon="search" @click="doQuery">查询</el-button>
            <el-button size="small" type="default" icon="refresh" @click="doReset">重置</el-button>
          </el-form-item>
        </el-form>
        <div class="return_btn">
          <el-tooltip content="返回"><img :src="back" alt="" @click="goBack" /></el-tooltip>
        </div>
      </div>
      <div class="table_div">
        <el-button size="small" type="primary" class="upload_btn" plain icon="Plus" @click="handleVisible('import')">OP2结果文件上传</el-button>
        <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
          <!-- <el-table-column type="selection" width="55" align="left" /> -->
          <el-table-column label="OP2文件名称" align="left" prop="fileName" fixed="left">
            <template #default="scope">
              <span>{{ scope.row.isfdLargeFileVo.fileName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="所属轮次" align="left" prop="roundNumber"> </el-table-column>
          <el-table-column label="文件所在位置" align="left" prop="location" width="220"> </el-table-column>
          <el-table-column label="备注" align="left" prop="remarks"> </el-table-column>
          <el-table-column label="上传日期" align="left" prop="modelNumber">
            <template #default="scope">
              <span>{{ scope.row.isfdLargeFileVo.uploadTimeString }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="上传人" align="left" prop="batchNumber"></el-table-column> -->
          <el-table-column label="文件上传状态" align="left" prop="remarks">
            <template #default="scope">
              <span>{{ statusMap[scope.row.isfdLargeFileVo.status] }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数据质量检测状态" align="left" prop="qualityCheck">
            <template #default="scope">
              <span>{{ scope.row.qualityCheck === 0 ? '通过' : '不通过' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left" width="100">
            <template #default="scope">
              <el-button v-if="scope.row.isfdLargeFileVo.status != 1" size="small" link type="primary" icon="View" @click="reUpload(scope.row)">重新上传</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagenation">
          <el-pagination
            v-show="total > 0"
            layout="prev, pager, next,jumper"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <ReUpload 
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      :row="fileRow"
      @handleEvent="handleEvent"
    />
  </div>
</template>
<script setup>
const statusMap = {'0': '未完成，可继续上传', '1': '上传成功', '2': '上传失败'}
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue';
import back from '@/assets/images/back.svg';
import ReUpload from './ReUpload.vue';
import { getCollectList, getMeshModelStructureList, getBatchNo, getOp2Tree, getCollectOp2DataList, deleteMeshOp2File } from '@/api/dataAcquisition';
// import AllFileView from './AllFileView.vue';
const emits = defineEmits(['handleEvent']);
const props = defineProps(['tableName']);
const resultLibraryTree = ref(null);
const tableData = ref([]);
const dialogVisible = ref(false);
const fileRow = ref([]);
const dateRangeCreateTime = ref(['', '']);
const currentChooseId = ref('');
const treeData = ref([]);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  modelNumber: '',
  batchNumber: '',
  qualityCheck: undefined,
  uploadTimeStartTime: '',
  uploadTimeEndTime: '',
  orderByColumn: 'createTime',
  isAsc: 'descending'
});
const modelNumberList = ref([]);
const batchList = ref([]);
const total = ref(0);
const loading = ref(false);
const queryName = ref('');

const filterTree = () => {
  loadTreeData(queryName.value); 
}

const handleCurrentChange = (number) => {
  queryParams.value.pageNum = number;
  doQuery();
};
const goBack = () => {  
  emits('handleEvent', { component: 'Main'});
}
// 回调函数
const handleEvent = (data) => {
  if (data?.type === 'refresh') {
    doQuery();
    loadTreeData();
    dialogVisible.value = false;
  } else if (data?.type === 'cancel') {
    dialogVisible.value = false;
  } 
};
// tree选择事件
const treeSelect = (data, node) => {
  // console.log(node, data)
  let sectionNumber = '', batchNumber = '', modelNumber = '';
  currentChooseId.value = data.id;
  if (node.level == 1) {
    sectionNumber = '';
    batchNumber = '';
    modelNumber = data.mark;
  } else if (node.level == 2) {
    sectionNumber = '';
    batchNumber = data.mark;
    modelNumber = node.parent.data.mark;
  } else if (node.level == 3) {
    sectionNumber = data.mark;
    batchNumber = node.parent.data.mark;
    modelNumber = node.parent.parent.data.mark;
  }
  doQuery({
    sectionNumber,
    batchNumber,
    modelNumber
  });
};
const reUpload = (row) => {
  fileRow.value = row;
  dialogVisible.value = true;
}
//查询数据列表
const doQuery = (params = {}) => {
  loading.value = true;
  tableData.value = [];
  queryParams.value.uploadTimeStartTime = dateRangeCreateTime.value[0];
  queryParams.value.uploadTimeEndTime = dateRangeCreateTime.value[1];
  params = {
    ...queryParams.value,
    ...params
  }
  
  getCollectOp2DataList(params)
    .then((res) => {
      // 处理数据
      loading.value = false;
      total.value = res?.total;
      tableData.value = res.rows;
    })
    .catch(() => {
      loading.value = false;
    }).finally(() => {
      loading.value = false;
    });
};
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
};

const doReset = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    modelNumber: '',
    batchNumber: '',
    qualityCheck: undefined,
    uploadTimeStartTime: '',
    uploadTimeEndTime: '',
    orderByColumn: 'createTime',
    isAsc: 'descending'
  };
  batchList.value = [];
  dateRangeCreateTime.value = ['', ''];
  doQuery();
};
// 打开弹窗方法
const handleVisible = (param, data) => {
  if (param == 'import') {
    emits('handleEvent', { component: 'ImportOp2' });
  }
};

//删除
const handleDelete = (row) => {
  const dataIds = row?.id ? [row?.id] : ids.value;
  // deleteBoundaryOptionValidate(dataIds).then(async res => {
  //   let infoMsg = !res.data ? res.msg : (row?.id ? '是否确认删除当前数据项？' : '是否确认删除当前所选全部数据项？')
  //   ElMessageBox.confirm(infoMsg, '提示', {
  //     confirmButtonText: '确定',
  //     cancelButtonText: '取消',
  //     type: 'warning'
  //   }).then(() => {
  //     deleteBoundaryOption(dataIds)
  //     .then((res) => {
  //       if (res) {
  //         ElMessage({ showClose: true, message: '删除成功。', type: 'success' });
  //         doQuery();
  //       }
  //     })
  //     .catch(() => {
  //       ElMessage({ showClose: true, message: '删除失败。', type: 'error' });
  //     });
  //   });
  // })
};
// 装载结果库树数据
const loadTreeData = (sectionName) => {
  loading.value = true;
  getOp2Tree({sectionName}).then(res => {
    treeData.value = res.data;
    loading.value = false;
  })
};
// 获取机型
const getModelList = () => {
  getMeshModelStructureList({parentId: 0, pageSize: 999}).then(res => {
    if (res) {
      modelNumberList.value = res.rows;
    }
  })
}

// 获取批次号
const modelChange = (val) => {
  if (val) {
    queryParams.value.batchNumber = '';
    getBatchNo({modelNumber: val}).then(res => {
      if (res) {    
        batchList.value = res.data;
      }
    })
  }
}
onMounted(() => {
  getModelList();
  loadTreeData();
  doQuery();
});
</script>
<style lang="scss" scoped>
.result-library-op2__main {
  display: flex;
  padding: 20px 5px 20px 20px;
  .left {
    flex: 0 0 300px;
    margin-right: 10px;
    height: calc(100vh - 130px);
    overflow-y: auto;
  }
  .right {
    flex: 1;
    height: calc(100vh - 130px);
    overflow: auto;
  }
  .query {
    position: relative;
    border: 1px solid #dddddd7a;
    padding: 20px 10px 0 10px;
    .query_btns {
      padding-left: 100px;
    }
    .return_btn {
      position: absolute;
      top: 30px;
      right: 30px;
      // width: 100%;
      > img {
        height: 45px;
        width: 45px;
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }
  .table_div {
    margin-top: 10px;
    border: 1px solid #dddddd7a;
    padding-bottom: 10px;
    .upload_btn {
      margin: 10px 10px;
    }
    .pagenation {
      display: flex;
      justify-content: end;
      margin-top: 10px;
    }
  }
  :deep(.el-form-item__content) {
    width: 220px;
  }
  :deep(.el-tree-node__label) {
    font-size: 12px;
    color: #606266;
  }
}
</style>
      